﻿package Server.Hnmj.ZZMJ;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.*;
import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;

public class MajTableMgr {
	static MajTableMgr __instance = new MajTableMgr();

	public static MajTableMgr instance() {
		return __instance;
	}

	Long m_LastDealTime;
	Long m_LastUpdateTime;

	static Map<Integer, MajTable> m_mapTable = new HashMap<>();// 房间号 . 房间
	Map<Integer, Long> m_mapHeartTime = new HashMap<>();// 心跳时间

	void Init() {
		m_LastDealTime = 0L;
		m_LastUpdateTime = 0L;
	}

	static MajTable FindTable(int _Uid) {
		MajTable it = (MajTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}

	void Handle_onHeart(int _Uid) {

	}

	void Handle_onConnect(int _Uid) {
	}

	void Handle_onJoin(int _Uid) {

	}

	static void Handle_onLeave(GameAgent agent, String[] vecArg) {

		Integer _Uid = agent.getUid();
		MajTable pTable = (MajTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true);
			
			
			QpGameMgr.getInstance().Handle_OnUserRoom(agent,new JSONObject());			
		}
	}

	void Handle_onDismissRoom(int _Uid) {

		MajTable tbl = FindTable(_Uid);
		if (tbl != null) {
			tbl.OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			tbl.DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(tbl);
		}

	}

	public static MajTable CreateTable(int _Uid, String strParam, boolean isFangGuan) {
		try {
			int byCostGem = 4;

			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;

			if (strParam == null)
				return null;

			String[] vecParam = strParam.split(";");

			if (vecParam.length == 0)
				return null;

			int jushu = 4;
			String payway = "AAPAY";

			String strRoomDesc = "郑州麻将  混子";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("4JU")) {
					jushu = 4;
					byCostGem = 4;
				}
				if (param.equalsIgnoreCase("8JU")) {
					jushu = 8;
					byCostGem = 8;
				}
				if (param.equalsIgnoreCase("16JU")) {
					jushu = 16;
					byCostGem = 16;
				}
			}
			
			int hutype = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("ZIMO")) {
					hutype = 1;
					strRoomDesc += " 自摸胡 ";
				}
				if (param.equalsIgnoreCase("DIANPAO")) {
					hutype = 0;
					strRoomDesc += " 允许点炮胡 ";					
				}
				
			}

			
			int hunPai = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("DHUN")) {
					hunPai = 1;
					strRoomDesc += " 有混 ";
				}
				if (param.equalsIgnoreCase("BDHUN")) {
					hunPai = 0;
					strRoomDesc += " 无混 ";					
				}
				
			}
			
			int duihufb = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("QDZFB")) {
					duihufb = 1;
					strRoomDesc += " 对胡翻倍 ";
				}
				if (param.equalsIgnoreCase("QDZBFB")) {
					duihufb = 0;
					strRoomDesc += " 对胡不翻倍 ";					
				}
				
			}
			
			int maxCost = byCostGem;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("AAPAY")) {
					maxCost = byCostGem * 1;
					strRoomDesc += " AA支付";
					payway = "AAPAY";
				}
				
				if (param.equalsIgnoreCase("FZPAY")) {
					
					maxCost = byCostGem * 4;
					strRoomDesc += " 房主支付";
					payway = "FZPAY";
				}
				
				if(param.equalsIgnoreCase("WINPAY")){
					maxCost = byCostGem * 4;
					strRoomDesc += "赢家支付";
					payway = "WINPAY";
				}
				if(param.equalsIgnoreCase("CLUBPAY")){
					maxCost = byCostGem * 4;
					byCostGem = maxCost;
					maxCost = 0;
					strRoomDesc += " 圈主支付";
					payway = "CLUBPAY";
				}
			}
			
			System.out.println("1[" + maxCost + "]1");
			if (jushu > 0 && byCostGem > 0 && payway.length() > 0 && user.getGem(0) >= maxCost) {
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {

					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					factor = rand_room;
					MajTable room = (MajTable) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new MajTable();
						room.Init(Constants.INIT_TOTAL);
						room.SetParam(strParam);
						room.SetDesc(strRoomDesc);
						room.m_diFen = 1;
						room.m_fdFen = 0;
						room.m_hunPai = hunPai;
						room.m_dhfb = duihufb;
						room.m_hutype = hutype;
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_payWay = payway;
						room.m_byMaxRound = (int) jushu;
						room.m_Creator = isFangGuan ? _Uid : 0;
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);

						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	void DismissTable(MajTable pTable) {

		if (pTable == null)
			return;

		QpGameMgr.getInstance().OnDismissRoom((IRoom) pTable);

		{

			m_mapTable.remove(pTable.GetID());
		}

	}

	void OnDeal(float dt) {
		Long tnow_ms = getCurrentTimeMillis();
		if (m_LastDealTime == 0) {
			m_LastDealTime = tnow_ms;
		}

		if (tnow_ms - m_LastDealTime >= 1000) {
			m_LastDealTime = tnow_ms;

		}
	}

	private Long getCurrentTimeMillis() {
		return System.currentTimeMillis();
	}

	public static void OnRecv(GameAgent agent, JSONObject root) {
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		Integer lUid = agent.getUid();

		SHMUser shmUser = dthall.getInstance().getUserInfo(lUid);
		if (shmUser == null) {
			return;
		}
		if (vecArg2.length > 0) {
			String subcmd = vecArg2[0];
			if (subcmd == "connect") {

			}
			if (subcmd.equals("join")) {

			} else if (subcmd.equals("leave")) {
				Handle_onLeave(agent,vecArg2);
			} else if (subcmd.equals("dismissroom")) {

			} else {
				IRoom iterUser = FindTable(lUid);
				if (iterUser == null) {
					return;
				}
				MajTable tbl = (MajTable) (iterUser);
				if (tbl != null) {
					if (subcmd.equals("ready")) {
						tbl.Handle_onReady(lUid, vecArg2);
					} else if (subcmd.equals("robot")) {
						tbl.Handle_onRobot(lUid, vecArg2);
					} else if (subcmd.equals("biaoqing")) {
						tbl.Handle_onBiaoQing(lUid, vecArg2);
					} else if (subcmd.equals("yuyin")) {
						tbl.Handle_onYuYin(lUid, vecArg2);
					} else if (subcmd.equals("chat")) {
						tbl.Handle_onChat(lUid, vecArg2);
					} else if (subcmd.equals("voice")) {
						tbl.Handle_onVoice(lUid, vecArg2);
					} else if (subcmd.equals("voicemsg")) {
						tbl.Handle_onVoiceMsg(lUid, vecArg2);
					} else if(subcmd.equals("expr")){
						tbl.Handle_onExpr(lUid, vecArg2);
					} else if (subcmd.equals("text")) {
						tbl.Handle_onText(lUid, vecArg2);
					} else if (subcmd.equals("mopai")) {
						tbl.Handle_onMoPai(lUid, vecArg2);
					} else if (subcmd.equals("chupai")) {
						tbl.Handle_onChuPai(lUid, vecArg2);
					} else if (subcmd.equals("chipai")) {
						tbl.Handle_onChiPai(lUid, vecArg2);
					} else if (subcmd.equals("pengpai")) {
						tbl.Handle_onPengPai(lUid, vecArg2);
					} else if (subcmd.equals("gangpai")) {
						tbl.Handle_onGangPai(lUid, vecArg2);
					} else if (subcmd.equals("guopai")) {
						tbl.Handle_onGuoPai(lUid, vecArg2);
					} else if (subcmd.equals("hupai")) {
						tbl.Handle_onHuPai(lUid, vecArg2);
					} else if (subcmd.equals("try_dismissroom")) {
						tbl.Handle_onTryDismissRoom(lUid, vecArg2);
					} else if (subcmd.equals("agree_dismissroom")) {
						tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
					} else if (subcmd.equals("refuse_dismissroom")) {
						tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
					}else if (subcmd.equals("inBack")){
						tbl.Handle_InBack(lUid,vecArg2);
					} else if (subcmd.equals("inFore")){
						tbl.Handle_InFore(lUid,vecArg2);
					}
				}
			}
		}

	}
}
